from pyecharts import options as opts
from pyecharts.charts import WordCloud
import jieba
with open('政府工作报告.txt', 'r') as file:
    text = file.read()
words = jieba.lcut(text)
word_counts = {}
for word in words:
    if len(word) > 1:
        word_counts[word] = word_counts.get(word, 0) + 1
word_k = dict(sorted(word_counts.items(), key=lambda item: item[1], reverse=True))
print(word_k)
WordCloud = WordCloud(init_opts=opts.InitOpts(page_title='词云图'))
WordCloud.add(series_name='',data_pair=word_counts.items(),word_size_range=[20, 60], shape='diamond')
WordCloud.set_global_opts(
    title_opts=opts.TitleOpts(
        title='政府工作报告词云图',
        pos_left='center'),
    legend_opts=opts.LegendOpts(is_show=False)
)
WordCloud.render('例8-1.html')